package com.zhiyume.controller;

import com.zhiyume.dao.entity.TCoupon;
import com.zhiyume.page.PageReq;
import com.zhiyume.service.TCouponService;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.shiro.AdminSecurityManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wang
 * @version $Id: CouponController.java, v 0.1 2018-04-12 14:56:39 wang Exp $$
 */
@Controller
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private TCouponService tCouponService;

    /**
     * 根据Id查询信息
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Map findById(long id){
        TCoupon coupon = tCouponService.findCouponById(id);

        if(null == coupon.getId()){
           return JsonUtil.returnResult(ConstantEnum.CONSTANT_MISS_PARA.getErrType(),ConstantEnum.CONSTANT_MISS_PARA.getErrMsg());
        }
        return JsonUtil.returnResult(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg(),coupon);
    }

    /**
     * 添加信息
     * @param tCoupon     * @return
     */
    @RequestMapping("/addCoupon")
    @ResponseBody
    public Map addCoupon(TCoupon tCoupon) {

        TCoupon coupon = new TCoupon();
        
        Long sessionId = AdminSecurityManager.getSessionTAdminSys().getId();
        tCoupon.setCreateId(sessionId);
        tCoupon.setUpdateId(sessionId);
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg(); 
        try{
            coupon = tCouponService.addCoupon(tCoupon);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }

        return JsonUtil.returnResult(errType,errMsg,coupon);

    }

    /**
     * page
     * @return
     */
    @RequestMapping("/couponListPage")
    public ModelAndView couponListPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/coupon/couponListPage");
        return modelAndView;
    }

    /**
     * 使用自定义mapper查询列表
     * @return
     */
    @RequestMapping("/getCouponDataList")
    @ResponseBody
    public Map<String,Object> getCouponDataList(TCoupon reqVO,PageReq page){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        couponData(reqVO, page, dataMap);
        return dataMap;
    }

    /**
     * 列表过滤查询
     * @return
     */
    @RequestMapping("/searchCouponList")
    @ResponseBody
    public Map<String,Object> searchCouponList(TCoupon reqVO, PageReq page){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        couponData(reqVO, page, dataMap);
        return dataMap;
    }
    
    private void couponData(TCoupon reqVO, PageReq page, Map<String, Object> dataMap) {
        List<TCoupon> list = tCouponService.queryCouponList(reqVO,page);
        list = list.size() > 0 ? list : null;
        int count = tCouponService.countCoupon(reqVO);
        dataMap.put("list",list);
        dataMap.put("total",count);
        dataMap.put("page",page);
    }
    
    @RequestMapping("/addCouponPage")
    public ModelAndView addCouponPage(Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/coupon/couponAddPage");
        TCoupon coupon = new TCoupon();
        coupon.settName("猫眼");
        model.addAttribute("coupon",coupon);
        List<String> sList = new ArrayList<>();
        sList.add("猫眼");
        sList.add("淘票票");
        sList.add("影火虫");
        sList.add("其他");
        model.addAttribute("sList",sList);
        return modelAndView;
    }

    @RequestMapping("/editCouponPage")
    public ModelAndView editPersonPage(@RequestParam("id") long id, Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/coupon/couponAddPage");
        TCoupon tCoupon = tCouponService.findCouponById(id);
        List<String> sList = new ArrayList<>();
        sList.add("猫眼");
        sList.add("淘票票");
        sList.add("影火虫");
        model.addAttribute("sList",sList);
        model.addAttribute("coupon", tCoupon);
        return modelAndView;
    }

    /**
     * 修改信息
     * @param tCoupon     * @return
     */
    @RequestMapping("/updateCoupon")
    @ResponseBody
    public Map updateCoupon(@ModelAttribute("form") TCoupon tCoupon) {
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg();
        try {
            TCoupon coupon = tCouponService.findCouponById(tCoupon.getId());
            BeanUtils.copyProperties(coupon,tCoupon,"ifUse");
             tCouponService.updateCoupon(tCoupon);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }
        return JsonUtil.returnResult(errType,errMsg);
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteCoupon")
    @ResponseBody
    public Map deleteCoupon(long id) {
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg();
        try {
            tCouponService.deleteCoupon(id);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }
        return JsonUtil.returnResult(errType,errMsg);
    }
}